﻿package components.control.sliderbar
{
    import components.display.Rect;

    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class SliderBar extends Sprite
    {


        /* 사용법
           sliderBar = new SliderBar(200, 10);
           sliderBar.addEventListener(Event.CHANGE, sliderBarChangeHandler);
           this.addChild(sliderBar);
         */

        private var _background:Sprite;
        private var _thumb:Sprite;
        private var _thumbSize:int;
        private var _maximum:Number;
        private var _minimum:Number;
        private var _percent:Number;
        private var _offset:int;
        private var _size:int;
        private var _width:int;

        public function SliderBar(size:int, width:int)
        {
            this._size = size;
            _width = width;

            init();

            this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
        }

        private function init():void
        {
            _background = new Sprite;
            this.addChild(_background);

            var bgRect:Rect = new Rect(_width, _size);
            bgRect.fillColor = 0xF5F5F5;
            _background.addChild(bgRect);

            _thumb = new Sprite;
            _thumb.x = 1;
            _thumb.y = 1;
            this.addChild(_thumb);

            var thumbRect:Rect = new Rect(_width - 2, 50);
            thumbRect.fillColor = 0x999999;
            _thumb.addChild(thumbRect);

            _maximum = _background.height - _thumb.height;
            _minimum = _thumb.y;
        }

        private function mouseDownHandler(event:MouseEvent):void
        {
            stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler, false, 0, true);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveHandler, false, 0, true);

            if(event.target == _thumb)
            {
                _offset = _thumb.mouseY;
            }
            else
            {
                _offset = _thumb.height >> 1;
            }

            move();
        }

        private function stageMouseUpHandler(event:MouseEvent):void
        {
            stage.removeEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler);
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveHandler);
            move();
        }

        private function stageMouseMoveHandler(event:MouseEvent):void
        {
            move();
        }

        private function move():void
        {
            var position:int = mouseY - _offset;
            if(position <= _minimum)
                position = _minimum;

            if(position >= _maximum)
                position = _maximum;

            _thumb.y = position;
            _percent = position / _maximum;

            dispatchEvent(new Event(Event.CHANGE));
        }

        public function getPercent():Number
        {
            return _percent;
        }

        public function setThumbSize(size:Number):void
        {

        }

        public function setPosition(position:int):void
        {
        }
    }
}